package fun.boomcat.luckyhe.mirai.plugin.botcontrol.util;

import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lucky_He
 * @date 2021年9月8日 13:49
 * @description BotInvitedJoinGroupRequestOperation
 */
public class BotInvitedJoinGroupRequestOperation {
    public static void botInvitedJoinGroupRequestOperation(CommandSender commandSender, Object[] args, String primaryName) {
        String operation = args[0].toString();

        long eventId;
        try {
            eventId = Long.parseLong(args[1].toString());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        switch (operation.toLowerCase()) {
            case "accept":
            case "接受":
                accept(commandSender, eventId);
                break;

            case "ignore":
            case "忽略":
                deny(commandSender, eventId);
                break;

            case "del":
            case "删除":
                delete(commandSender, eventId);
                break;

            default:
                commandSender.sendMessage(ControlCommandHelp.botInvitedJoinGroupRequestHelp(primaryName));
                break;
        }
    }

    public static void accept(CommandSender commandSender, long eventId) {
        BotInvitedJoinGroupRequestEvent event;
        try {
            event = BotInvitedJoinGroupRequestEventUtil.get(eventId);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        try {
            event.accept();
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已同意事件号为" + eventId + "的邀请进群请求");
    }

    public static void deny(CommandSender commandSender, long eventId) {
        BotInvitedJoinGroupRequestEvent event;
        try {
            event = BotInvitedJoinGroupRequestEventUtil.get(eventId);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        try {
            event.ignore();
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已忽略事件号为" + eventId + "的邀请进群请求");
    }

    public static void delete(CommandSender commandSender, long eventId) {
        BotInvitedJoinGroupRequestEvent event;
        try {
            event = BotInvitedJoinGroupRequestEventUtil.get(eventId);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        BotInvitedJoinGroupRequestEventUtil.remove(event);

        commandSender.sendMessage("已删除事件号为" + eventId + "的邀请进群请求");
    }
}
